const Base = require("./base.js");

module.exports = class extends Base {
  async loginAction() {
    const sT = Date.now();
    const post = this.post();
    const db = this.model("user");
    let result = {};
    const user = await db
      .where({
        name: post.name
      })
      .find();
    if (think.isEmpty(user)) {
      result.errno = 1;
    } else if (user.pass !== post.pass) {
      result.errno = 2;
    } else {
      result.errno = 0;
      delete user.pass;
      const token = think.uuid();
      await this.cache(token, user);
      result.data = user;
      result.token = token;
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }
  /**
   * 列表
   * @param name
   * @returns {Promise<any>}
   */
  async indexAction() {
    const sT = Date.now();
    const post = this.post();
    const db = this.model("user");
    let result = {};
    result.errno = 0;
    result.data = await db.list(post);
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 删除
   * @param id
   * @returns {Promise<void>}
   */
  async delAction() {
    const post = this.post();
    const sT = Date.now();
    const db = this.model("user");
    const dbContent = this.model("user_meta");
    let result = {};
    try {
      await db.delete(post.id);
      await dbContent.where({ user_id: post.id }).delete();
      result.errno = 0;
    } catch (e) {
      result.errno = -1;
      result.errmsg = e.toString();
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 添加
   * @returns {Promise<any>}
   */
  async addAction() {
    const sT = Date.now();
    const db = this.model("user");
    const dbMeta = this.model("user_meta");
    const post = this.post();
    let result = {};
    try {
      result.data = await db.add({
        ...post,
        ctime: ["exp", "now()"],
        etime: ["exp", "now()"]
      });
      await dbMeta.saveByUserId({ user_id: result.data, ...post });
      result.errno = 0;
    } catch (e) {
      result.errno = -1;
      result.errmsg = e.toString();
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 详情
   * @returns {Promise<any>}
   */
  async detailAction() {
    const sT = Date.now();
    const post = this.post();
    const db = this.model("user");
    let result = {};
    try {
      result.errno = 0;
      result.data = await db.detail(post);
    } catch (e) {
      result.errno = -1;
      result.errmsg = e.toString();
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }

  /**
   * 修改
   * @returns {Promise<any>}
   */
  async updateAction() {
    const sT = Date.now();
    const { id, ...form } = this.post();
    let result = {};
    if (!form || Object.keys(form).length === 0) {
      result.errno = -1;
      result.errmsg = "没有任何修改项";
    } else {
      const db = this.model("user");
      const dbMeta = this.model("user_meta");
      try {
        result.data = await db.where({ id }).update({
          ...form,
          etime: ["exp", "now()"]
        });
        await dbMeta.saveByUserId({ user_id: id, ...form });
        result.errno = 0;
      } catch (e) {
        result.errno = -1;
        result.errmsg = e.toString();
      }
    }
    result.time = Date.now() - sT;
    return this.json(this.format(result));
  }
};
